package com.qingpeng.pay.activity;

import android.os.CountDownTimer;
import android.text.InputType;
import android.text.Spanned;
import android.text.method.DigitsKeyListener;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.qingpeng.pay.R;
import com.qingpeng.pay.api.Host;
import com.qingpeng.pay.api.RedBag;
import com.qingpeng.pay.base.BaseActivity;
import com.qingpeng.pay.beans.LoginBean;
import com.qingpeng.pay.beans.NullBean;
import com.qingpeng.pay.okhttp.RetrofitCreateHelper;
import com.qingpeng.pay.utils.APP;
import com.qingpeng.pay.utils.SpUtils;
import com.qingpeng.pay.utils.StringUtils;
import com.qingpeng.pay.utils.TimerCountUtils;
import com.qingpeng.pay.utils.ToastUtils;

import java.util.HashMap;

import butterknife.BindView;
import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class LoginActivity extends BaseActivity {


    @BindView(R.id.ed_account)
    EditText edAccount;
    @BindView(R.id.ed_pwd)
    EditText edPwd;
    @BindView(R.id.ll_pwd)
    LinearLayout llPwd;
    @BindView(R.id.ed_yzm)
    EditText edYzm;
    @BindView(R.id.btn_yzm)
    TextView btnYzm;
    @BindView(R.id.ll_yzm)
    LinearLayout llYzm;
    @BindView(R.id.ed_invite)
    EditText edInvite;
    @BindView(R.id.pay)
    TextView pay;
    @BindView(R.id.registe)
    TextView registe;
    @BindView(R.id.no_pwd)
    TextView noPwd;
    @BindView(R.id.ll_login)
    LinearLayout llLogin;
    @BindView(R.id.registe_account)
    EditText registeAccount;
    @BindView(R.id.registe_pwd)
    EditText registePwd;
    @BindView(R.id.registe_pwds)
    EditText registePwds;
    @BindView(R.id.registe_invite)
    EditText registeInvite;
    @BindView(R.id.registe_pay)
    TextView registePay;
    @BindView(R.id.ll_registe)
    LinearLayout llRegiste;
    private boolean isLogin;
    private int type;
    private CountDownTimer timer;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_login;
    }

    @Override
    protected int getRootViewId() {
        return 0;
    }

    @Override
    protected int getRefreshId() {
        return 0;
    }

    @Override
    protected int getListViewId() {
        return 0;
    }


    @Override
    protected void onResume() {
        super.onResume();
        type = 1;
        isLogin = SpUtils.getBoolean(LoginActivity.this, "islogin", false);
        if (isLogin) {
            String uid = SpUtils.getString(LoginActivity.this, "uid", "");
            String toke = SpUtils.getString(LoginActivity.this, "toke", "");
            String userInfo = SpUtils.getString(LoginActivity.this, "userInfo", "");
            if (!StringUtils.isEmpty(uid) && !StringUtils.isEmpty(toke)) {
                APP.uid = uid;
                APP.Token = toke;
                APP.userInfo = new Gson().fromJson(userInfo, LoginBean.DataProductBean.class);
                startActivity(MainActivity.class);
                LoginActivity.this.finish();
            }
        }

        registeInvite.setInputType(InputType.TYPE_NUMBER_FLAG_DECIMAL | InputType.TYPE_CLASS_NUMBER);

        registeInvite.setKeyListener(new DigitsKeyListener() {

            @Override
            public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
                return source;
            }
        });
        edPwd.setInputType(InputType.TYPE_NUMBER_FLAG_DECIMAL | InputType.TYPE_CLASS_NUMBER);

        edPwd.setKeyListener(new DigitsKeyListener() {

            @Override
            public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
                return source;
            }
        });
        registePwd.setInputType(InputType.TYPE_NUMBER_FLAG_DECIMAL | InputType.TYPE_CLASS_NUMBER);

        registePwd.setKeyListener(new DigitsKeyListener() {

            @Override
            public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
                return source;
            }
        });
        registePwds.setInputType(InputType.TYPE_NUMBER_FLAG_DECIMAL | InputType.TYPE_CLASS_NUMBER);

        registePwds.setKeyListener(new DigitsKeyListener() {

            @Override
            public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
                return source;
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (timer != null) {
            TimerCountUtils.status = false;
            timer.cancel();
        }
    }

    @OnClick({R.id.ed_account, R.id.ed_pwd, R.id.ll_pwd, R.id.ed_yzm, R.id.btn_yzm, R.id.ll_yzm, R.id.ed_invite, R.id.pay, R.id.registe, R.id.no_pwd, R.id.ll_login, R.id.registe_account, R.id.registe_pwd, R.id.registe_pwds, R.id.registe_invite, R.id.registe_pay, R.id.ll_registe})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_yzm:
                if (!TimerCountUtils.status) {
                    timer = TimerCountUtils.getTimer(btnYzm);
                    timer.start();
                    Call<NullBean> sendcode = RetrofitCreateHelper.createApi(RedBag.class, Host.HOST).sendcode(edAccount.getText().toString());
                    sendcode.enqueue(new Callback<NullBean>() {
                        @Override
                        public void onResponse(Call<NullBean> call, Response<NullBean> response) {
                            NullBean body = response.body();
                            if (body.getStatus() != 1) {
                                ToastUtils.showToast(LoginActivity.this, body.getInfo());
                                return;
                            }
                            ToastUtils.showToast(LoginActivity.this, body.getInfo());
                        }

                        @Override
                        public void onFailure(Call<NullBean> call, Throwable t) {
                            ToastUtils.showToast(LoginActivity.this, "网络请求错误,请稍后重试");
                        }
                    });
                }

                break;
            case R.id.pay:
                if (type == 1) {
                    Call<LoginBean> userinfo = RetrofitCreateHelper.createApi(RedBag.class, Host.HOST).login(edAccount.getText().toString(), edPwd.getText().toString());
                    userinfo.enqueue(new Callback<LoginBean>() {
                        @Override
                        public void onResponse(Call<LoginBean> call, Response<LoginBean> response) {
                            LoginBean body = response.body();
                            if (body.getStatus() != 1) {
                                ToastUtils.showToast(LoginActivity.this, body.getInfo());
                                return;
                            }
                            LoginBean.DataProductBean data = body.getData();
                            APP.uid = data.getUser_id();
                            APP.Token = data.getToken();
                            APP.userInfo = body.getData();
                            SpUtils.putBoolean(LoginActivity.this, "islogin", true);
                            SpUtils.putString(LoginActivity.this, "uid", data.getUser_id());
                            SpUtils.putString(LoginActivity.this, "toke", data.getToken());
                            SpUtils.putString(LoginActivity.this, "userInfo", new Gson().toJson(body.getData()));
                            startActivity(MainActivity.class);
                            LoginActivity.this.finish();
                        }

                        @Override
                        public void onFailure(Call<LoginBean> call, Throwable t) {
                            ToastUtils.showToast(LoginActivity.this, "网络请求错误,请稍后重试");
                        }
                    });
                } else {
                    Call<LoginBean> userinfo = RetrofitCreateHelper.createApi(RedBag.class, Host.HOST).mobilelogin(edAccount.getText().toString(), edYzm.getText().toString());
                    userinfo.enqueue(new Callback<LoginBean>() {
                        @Override
                        public void onResponse(Call<LoginBean> call, Response<LoginBean> response) {
                            LoginBean body = response.body();
                            if (body.getStatus() != 1) {
                                ToastUtils.showToast(LoginActivity.this, body.getInfo());
                                return;
                            }
                            LoginBean.DataProductBean data = body.getData();
                            APP.uid = data.getUser_id();
                            APP.Token = data.getToken();
                            APP.userInfo = body.getData();
                            SpUtils.putBoolean(LoginActivity.this, "islogin", true);
                            SpUtils.putString(LoginActivity.this, "uid", data.getUser_id());
                            SpUtils.putString(LoginActivity.this, "toke", data.getToken());
                            SpUtils.putString(LoginActivity.this, "userInfo", new Gson().toJson(body.getData()));
                            startActivity(MainActivity.class);
                            LoginActivity.this.finish();
                        }

                        @Override
                        public void onFailure(Call<LoginBean> call, Throwable t) {
                            ToastUtils.showToast(LoginActivity.this, "网络请求错误,请稍后重试");                        }
                    });
                }
                break;
            case R.id.registe:
                llLogin.setVisibility(View.GONE);
                llRegiste.setVisibility(View.VISIBLE);
                break;
            case R.id.no_pwd:
                type = 2;
                llPwd.setVisibility(View.GONE);
                llYzm.setVisibility(View.VISIBLE);
                break;
            case R.id.registe_pay:
                HashMap<String, String> map = new HashMap<>();
                if (!registePwd.getText().toString().equals(registePwds.getText().toString())) {
                    ToastUtils.showToast(LoginActivity.this, "两次密码不相同");
                    return;
                }
                map.put("mobile", registeAccount.getText().toString());
                map.put("code", registeInvite.getText().toString());
                map.put("pwd", registePwd.getText().toString());
                map.put("repwd", registePwds.getText().toString());
                Call<LoginBean> registe = RetrofitCreateHelper.createApi(RedBag.class, Host.HOST).mobilelogin(map);
                registe.enqueue(new Callback<LoginBean>() {
                    @Override
                    public void onResponse(Call<LoginBean> call, Response<LoginBean> response) {
                        LoginBean body = response.body();
                        if (body.getStatus() != 1) {
                            ToastUtils.showToast(LoginActivity.this, body.getInfo());
                            return;
                        }
                        LoginBean.DataProductBean data = body.getData();
                        APP.uid = data.getUser_id();
                        APP.Token = data.getToken();
                        APP.userInfo = body.getData();
                        SpUtils.putBoolean(LoginActivity.this, "islogin", true);
                        SpUtils.putString(LoginActivity.this, "uid", data.getUser_id());
                        SpUtils.putString(LoginActivity.this, "toke", data.getToken());
                        SpUtils.putString(LoginActivity.this, "userInfo", new Gson().toJson(body.getData()));
                        startActivity(MainActivity.class);
                        LoginActivity.this.finish();
                    }

                    @Override
                    public void onFailure(Call<LoginBean> call, Throwable t) {
                        ToastUtils.showToast(LoginActivity.this, "网络请求错误,请稍后重试");                    }
                });
                break;
        }
    }
}
